# -*- coding: utf-8 -*-


def des3():
    """3DES"""
    # pip install pycryptodome
    from Crypto.Cipher import DES3
    from Crypto.Util.Padding import pad

    key = "0123456789ABCDEF12345678"
    txt = "0123456789ABCDEF12345678"
    data = bytes(txt, encoding="utf-8")

    print(type(data))

    des3 = DES3.new(key=key, mode=DES3.MODE_ECB)
    text = pad(data_to_pad=data, block_size=DES3.block_size)
    print(type(text))
    temp = des3.encrypt(plaintext=text)

    print(type(temp))
    print(temp.hex().upper())
    print(str(des3.decrypt(temp), encoding="utf-8").strip(chr(8)).__len__())

    pass


def sm4():
    """SM4"""
    # pip install gmssl
    from gmssl.sm4 import CryptSM4, SM4_ENCRYPT, SM4_DECRYPT

    key = b'0123456789abcdef'
    value = b'{"key":"value","date_time":"2020-06-26 12:30:00"}'  # bytes类型
    iv = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'  # bytes类型
    crypt_sm4 = CryptSM4()
    crypt_sm4.set_key(key, SM4_ENCRYPT)
    encrypt_value = crypt_sm4.crypt_ecb(value)  # bytes类型
    crypt_sm4.set_key(key, SM4_DECRYPT)
    decrypt_value = crypt_sm4.crypt_ecb(encrypt_value)  # bytes类型

    print(bytearray.hex(bytearray(encrypt_value)))
    print(bytearray.hex(bytearray(decrypt_value)))

    assert value == decrypt_value

    pass


def rsa():
    """RSA"""
    # 需要安装第三方模块rsa
    import rsa

    data = '0123456789abcdef'

    modulus = 'A1E4D93618B8B240530853E87738403851E15BBB77421F9B2377FB0B4F1C6FC235EAEC92EA25BB76AC221DCE90173A2E232FE1511909C76B15251D4059B288E709C1EF86BCF692757AAD736882DD1E98BEDFED9311A3C22C40657C9A52880BDC4B9E539041D44D52CB26AD13AB086F7DC294D144D6633A62EF91CA1775EB9A09'
    exponent = '010001'  # 0x010001 65537

    publicKey = rsa.PublicKey(int(modulus, 16), int(exponent, 16))
    print(publicKey.save_pkcs1())
    # 加密
    encrypt = rsa.encrypt(data.encode('utf8'), publicKey)
    print(encrypt)

    # 验签
    verify = rsa.verify(data.encode('utf-8'), publicKey)
    print(verify)

    # with open('./private_key.pem' ,'r') as pem_file:
    #     privateKey = rsa.PrivateKey.load_pkcs1(pem_file, format='PEM')

    #     # 解密
    #     decrypt = rsa.decrypt(encrypt, privateKey)
    #     print(decrypt)

    #     # 加签
    #     sign = rsa.sign(data.encode('utf-8'), privateKey, 'SHA-1')
    #     print(sign)

    #     pass

    from Crypto.PublicKey import RSA
    # 生成密钥

    pass


if __name__ == "__main__":

    # md5()

    # base64()

    # des3()

    # sm4()

    # rsa()

    pass
